package com.hrm.service;

import com.hrm.dao.EmployeeDao;
import com.hrm.dao.InfoDao;
import com.hrm.dao.SalaryDao;
import com.hrm.dto.Envelope;
import com.hrm.entity.DepartmentEntity;
import com.hrm.entity.EmployeeEntity;
import com.hrm.entity.InfoEntity;
import com.hrm.entity.SalaryEntity;
import com.hrm.util.DateUtil;
import com.hrm.util.JSONUtil;
import com.hrm.util.PasswordUtil;
import net.sf.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by zekdot on 20-2-24.
 */
public class EmployeeService {

    private EmployeeDao employeeDao;

    private SalaryDao salaryDao;

    private InfoDao infoDao;

    public EmployeeService(){
        employeeDao = new EmployeeDao();
        salaryDao = new SalaryDao();
        infoDao = new InfoDao();
    }

    /**
     * 获取员工
     * @param id
     * @return
     */
    public Envelope getEmployee(String id){
        EmployeeEntity employeeEntity = null;
        try {
            employeeEntity = employeeDao.get(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        if(employeeEntity == null){
            return new Envelope(1,"员工不存在",null);
        }
        employeeEntity.setPassword(null);
//        InfoEntity infoEntity = null;
//        try {
//            infoEntity = infoDao.get(id);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Envelope.dbError;
//        }
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("info",infoEntity);
//        jsonObject.put("employee",employeeEntity);
        return new Envelope(employeeEntity);
    }
    /**
     * 登录
     * @param id
     * @param password
     * @return
     */
    public Envelope login(String id,String password){
        EmployeeEntity employeeEntity = null;
        try {
            employeeEntity = employeeDao.get(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(employeeEntity == null || !employeeEntity.getPassword().equals(PasswordUtil.getMd5(password))){
            return new Envelope(1,"用户名或密码错误",null);
        }
        employeeEntity.setPassword(null);
        return new Envelope(employeeEntity);
    }
    /**
     * 修改雇员密码
     * @param id 管理员id
     * @param oriPassword 原密码
     * @param newPassword 新密码
     * @return
     */
    public Envelope changePassword(String id,String oriPassword,String newPassword){
        EmployeeEntity employeeEntity;
        try {
            employeeEntity = employeeDao.get(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        if(!employeeEntity.getPassword().equals(PasswordUtil.getMd5(oriPassword))){
            return new Envelope(1,"原密码输入错误",null);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("password",PasswordUtil.getMd5(newPassword));
        try {
            employeeDao.update(id,map);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return Envelope.success;
    }
    /**
     * 获取雇员信息
     * @param id 雇员id
     * @return
     */
    public Envelope getInfo(String id){
        InfoEntity infoEntity = null;
        try {
            infoEntity = infoDao.get(id);
            if(infoEntity == null){
                infoEntity = new InfoEntity();
                infoEntity.setName("");
                infoEntity.setGender("女");
                infoEntity.setAge(0);
                infoEntity.setContact("");
                infoEntity.setPolity("");
                infoEntity.setNation("");
                infoEntity.setOutDate("");
                infoEntity.setOrigin("");
                infoEntity.setInDate("");
                infoEntity.setEId(id);
                infoEntity.setGraduation("");
                infoEntity.setEducation("");
                infoDao.save(infoEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return new Envelope(infoEntity);
    }

    /**
     * 更新雇员信息
     * @param id
     * @param paramJson 参数JSON
     * @return
     */
    public Envelope updateInfo(String id,String paramJson){
        JSONObject json = JSONObject.fromObject(paramJson);

        Map<String,Object> map = JSONUtil.getParamJson(json);
        try {
            infoDao.update(id,map);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return Envelope.success;
    }




    /**
     * 调动雇员的职位
     * @param id 雇员id
     * @param newDep 雇员的新部门
     * @param newPos 雇员的新职位
     * @return
     */
    public Envelope transfer(String id,String newDep,String newPos){
        EmployeeEntity employeeEntity;
        try {
            employeeEntity = employeeDao.get(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("position",newPos);
        map.put("department",newDep);
        try {
            employeeDao.update(id,map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Envelope.success;
    }

    /**
     * 让某雇员离职
     * @param id 雇员id
     * @return
     */
    public Envelope leave(String id){
        Map<String,Object> map = new HashMap<>();
        map.put("status",1);
        try {
            employeeDao.update(id,map);
            map = new HashMap<>();
            map.put("outDate", DateUtil.date2Str(new Date()));
            infoDao.update(id,map);
            return Envelope.success;
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
    }

    /**
     * 增加一个雇员
     * @param id 雇员id
     * @param dep 部门
     * @param pos 职位
     * @return 结果
     */
    public Envelope addEmployee(String id,String dep,String pos){
        InfoEntity infoEntity;
        infoEntity = new InfoEntity();
        infoEntity.setName("");
        infoEntity.setGender("女");
        infoEntity.setAge(0);
        infoEntity.setContact("");
        infoEntity.setPolity("");
        infoEntity.setNation("");
        infoEntity.setOutDate("");
        infoEntity.setOrigin("");
        infoEntity.setInDate(DateUtil.date2Str(new Date()));
        infoEntity.setEId(id);
        infoEntity.setGraduation("");
        infoEntity.setEducation("");

        EmployeeEntity employeeEntity = new EmployeeEntity();
        employeeEntity.setId(id);
        employeeEntity.setDepartment(dep);
        employeeEntity.setPosition(pos);
        employeeEntity.setPassword(PasswordUtil.getMd5("123456"));
        employeeEntity.setStatus(0);
        employeeEntity.setAuth("n");
        try {
            employeeDao.save(employeeEntity);
            infoDao.save(infoEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return Envelope.success;
    }

    /**
     * 删除一个雇员
     * @param id 雇员id
     * @return
     */
    public Envelope deleteEmployee(String id){
        try {
            employeeDao.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return Envelope.success;
    }

    /**
     * 获取员工的薪水
     * @param id
     * @return
     */
    public Envelope getSalary(String id,String year,String month){
        SalaryEntity salaryEntity = null;
        try {
            salaryEntity = salaryDao.get(id,year,month);
            if(salaryEntity == null){
                salaryEntity = new SalaryEntity();
                salaryEntity.setYear(year);
                salaryEntity.setMonth(month);
                salaryEntity.setBase(0);
                salaryEntity.setEId( id);
                salaryEntity.setDeduct(0);
                salaryEntity.setComLevel("");
                salaryEntity.setBonus(0);
                salaryDao.save(salaryEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return new Envelope(salaryEntity);
    }

    /**
     * 更新员工的薪水
     * @param id 员工id
     * @param year 年份
     * @param month  月份
     * @param paramJson 参数
     * @return
     */
    public Envelope updateSalary(String id,String year,String month,String paramJson){
        JSONObject json = JSONObject.fromObject(paramJson);
        Map<String,Object> map = JSONUtil.getParamJson(json);
        try {
            salaryDao.update(id,year,month,map);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
        return Envelope.success;
    }

    /**
     * 获取所有雇主信息
     * @return
     */
    public Envelope getEmployers(){
        try{
            return new Envelope(employeeDao.getEmployers());
        }catch(Exception e){
            e.printStackTrace();
            return Envelope.dbError;
        }
    }

    /**
     * 获取本部门的所有员工
     * @param dep 部门名称
     * @return
     */
    public Envelope getUnderEmployees(String dep){
        try {
            return new Envelope(employeeDao.getUnderEmployee(dep));
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
    }
    public static void main(String args[]){
        EmployeeService employeeService = new EmployeeService();
//        System.out.println(employeeService.infoList("",30));
//        JSONObject jsonObject = JSONObject.fromObject("{\"test\":2}");
//        System.out.println(jsonObject.getInt("test"));
//        System.out.println(employeeService.getSalary("Ryfb2020123","2020","03"));
//        String str = DateUtil.date2Str(new Date());
//        String data[] = str.split("-");
//        System.out.println(data[0]);
//        System.out.println(data[1]);
        System.out.println(employeeService.getEmployers());
        System.out.println(employeeService.getUnderEmployees("产品研发部"));
    }
}
